﻿using Dao;
using MB.RuleBase.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Business
{
    public class BBoxOrder
    {

        /// <summary>
        /// 绑定商品和周转箱关系
        /// </summary>
        /// <param name="goodscode"></param>
        /// <param name="zboxbno"></param>
        /// <returns></returns>
        public bool SetZboxSkuRelation( int goodsid, string zboxbno,int UserID, string UserName)
        {
            bool _result = false;
            string _message = "";
            var oracle = new MB.RuleBase.BulkCopy.SimulatedOracleHelper();
            var db = MB.Orm.Persistence.DatabaseHelper.CreateDatabase();
            var cn = oracle.CreateOracleConnection(db.ConnectionString);
            System.Data.Common.DbTransaction tran = null;
            try
            {
                // 打开数据连接
                cn.Open();
                tran = cn.BeginTransaction();
                try
                {
                    _result = DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery(tran, "ZboxGoods", "InsertZboxSkuRe", goodsid, zboxbno) > 0;
                    _result = DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery(tran, "ZboxGoods", "InsertZboxSkuReHIS", goodsid, zboxbno, UserID, UserName) > 0;
                }
                catch (Exception ex)
                {
                    MB.Util.TraceEx.Write(ex.Message);
                    if (ex.Message.Contains("ORA-03135"))
                    {
                        _result = DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery(tran, "ZboxGoods", "InsertZboxSkuRe", goodsid, zboxbno) > 0;
                        _result = DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery(tran, "ZboxGoods", "InsertZboxSkuReHIS", goodsid, zboxbno,UserID, UserName) > 0;
                    }
                }
                _result = true;
                if (_result)
                { tran.Commit(); }
                else
                { tran.Rollback(); }
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                tran.Rollback();
                _message = ex.Message;
                _result = false;
            }
            finally
            {
                if (tran != null) tran.Dispose();
                if (cn != null) cn.Close();
                if (cn != null) cn.Dispose();
            }

            return _result;
        }


        /// <summary>
        /// 绑定订单和发货箱关系
        /// </summary>
        /// <returns></returns>
        public bool SetFboxOrderRelation(string OrderNO, string FboxNO, int UserID, string UserName)
        {
            bool _result = false;
            string _message = "";
            int BinbdFlag=1;
            var oracle = new MB.RuleBase.BulkCopy.SimulatedOracleHelper();
            var db = MB.Orm.Persistence.DatabaseHelper.CreateDatabase();
            var cn = oracle.CreateOracleConnection(db.ConnectionString);
            System.Data.Common.DbTransaction tran = null;
            try
            {
                // 打开数据连接
                cn.Open();
                tran = cn.BeginTransaction();
                try
                {
                    _result = DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery(tran, "FboxOrder", "InsertFboxOrderRe", OrderNO, FboxNO) > 0;
                    _result = DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery(tran, "FboxOrder", "InsertFboxOrderReHIS", OrderNO, FboxNO, UserID, UserName) > 0;//历史记录
                    _result = _result & DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery(tran, "FboxOrder", "UpdatePindentStatus", BinbdFlag, OrderNO) > 0;
                }
                catch (Exception ex)
                {
                    MB.Util.TraceEx.Write(ex.Message);
                    if (ex.Message.Contains("ORA-03135"))
                    {
                        _result = DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery(tran, "FboxOrder", "InsertFboxOrderRe", OrderNO, FboxNO) > 0;
                        _result = DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery(tran, "FboxOrder", "InsertFboxOrderReHIS", OrderNO, FboxNO, UserID, UserName) > 0;//历史记录
                        _result = _result & DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery(tran, "FboxOrder", "UpdatePindentStatus", BinbdFlag, OrderNO) > 0;
                    }
                }
                _result = true;
                if (_result)
                { tran.Commit(); }
                else
                { tran.Rollback(); }
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                tran.Rollback();
                _message = ex.Message;
                _result = false;
            }
            finally
            {
                if (tran != null) tran.Dispose();
                if (cn != null) cn.Close();
                if (cn != null) cn.Dispose();
            }

            return _result;
        }

        public bool deleteFboxOrderrelation(string OrderNO, int UserID, string UserName)
        {
            bool _result = false;
            string _message = "";
            var oracle = new MB.RuleBase.BulkCopy.SimulatedOracleHelper();
            var db = MB.Orm.Persistence.DatabaseHelper.CreateDatabase();
            var cn = oracle.CreateOracleConnection(db.ConnectionString);
            System.Data.Common.DbTransaction tran = null;
            try
            {
                // 打开数据连接
                cn.Open();
                tran = cn.BeginTransaction();
                List<string> FboxNOs = OrderDao.NewInstance.GetFboxsByOrderNO(OrderNO);
                try
                {
                    _result = DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery(tran, "FboxOrder", "deleteFboxOrderRelation", OrderNO) > 0;
                    if (FboxNOs != null && FboxNOs.Count > 0)
                    {
                        foreach (string fboxno in FboxNOs)
                        {
                            _result = DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery(tran, "FboxOrder", "InsertFboxOrderReHIS", "delete-" + OrderNO, "delete-" + fboxno, UserID, UserName) > 0;//历史记录
                        }
                    }
                }
                catch (Exception ex)
                {
                    MB.Util.TraceEx.Write(ex.Message);
                    if (ex.Message.Contains("ORA-03135"))
                    {
                        _result = DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery(tran, "FboxOrder", "deleteFboxOrderRelation", OrderNO) > 0;
                        if (FboxNOs != null && FboxNOs.Count > 0)
                        {
                            foreach (string fboxno in FboxNOs)
                            {
                                _result = DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery(tran, "FboxOrder", "InsertFboxOrderReHIS", "delete-" + OrderNO, "delete-" + fboxno, UserID, UserName) > 0;//历史记录
                            }
                        }
                    }
                }
                _result = true;
                if (_result)
                { tran.Commit(); }
                else
                { tran.Rollback(); }
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                tran.Rollback();
                _message = ex.Message;
                _result = false;
            }
            finally
            {
                if (tran != null) tran.Dispose();
                if (cn != null) cn.Close();
                if (cn != null) cn.Dispose();
            }

            return _result;
        }

        public bool deleteFboxOrderPair(string OrderNO,string FboxNO, int UserID, string UserName)
        {
            bool _result = false;
            string _message = "";
            var oracle = new MB.RuleBase.BulkCopy.SimulatedOracleHelper();
            var db = MB.Orm.Persistence.DatabaseHelper.CreateDatabase();
            var cn = oracle.CreateOracleConnection(db.ConnectionString);
            System.Data.Common.DbTransaction tran = null;
            try
            {
                // 打开数据连接
                cn.Open();
                tran = cn.BeginTransaction();
                try
                {
                    _result = DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery(tran, "FboxOrder", "deleteFboxOrderPair", OrderNO, FboxNO) > 0;
                    _result = DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery(tran, "FboxOrder", "InsertFboxOrderReHIS", "delete-" + OrderNO, "delete-" + FboxNO, UserID, UserName) > 0;//历史记录
                }
                catch (Exception ex)
                {
                    MB.Util.TraceEx.Write(ex.Message);
                    if (ex.Message.Contains("ORA-03135"))
                    {
                        _result = DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery(tran, "FboxOrder", "deleteFboxOrderPair", OrderNO, FboxNO) > 0;
                        _result = DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery(tran, "FboxOrder", "InsertFboxOrderReHIS", "delete-" + OrderNO, "delete-" + FboxNO, UserID, UserName) > 0;//历史记录
                    }
                }
                _result = true;
                if (_result)
                { tran.Commit(); }
                else
                { tran.Rollback(); }
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                tran.Rollback();
                _message = ex.Message;
                _result = false;
            }
            finally
            {
                if (tran != null) tran.Dispose();
                if (cn != null) cn.Close();
                if (cn != null) cn.Dispose();
            }

            return _result;
        }
    }
}
 